/*
*** (C) 2008 UCMai.com ***

Subj: 扩展GridPanel对象
Auth: zhouyongtai
Date: 2011-01-29
*/
Ext.namespace("Ext.ux.grid");
Ext.ux.grid.GridPanel = Ext.extend(Ext.grid.GridPanel, {
	loadMask: {msg:c.g(c.e.load)},
	pageSize: c.page.LargePageSize,

	newStore: function(config){ 
		return new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({url: config.url}),
			reader: config.reader,
			baseParams: {limit: this.pageSize}
		});
	},
	//private 方法,初始化函数
	initComponent: function(){
		if(!Ext.isEmpty(this.gridHeader)) {
			this.initStructure();
		}
		//加上这一句，不然会产生异常。
		Ext.ux.grid.GridPanel.superclass.initComponent.call(this);
	},
	initEvents : function() {
		Ext.ux.grid.GridPanel.superclass.initEvents.call(this);
		
		//if (this.loadMask) {
			//this.loadMask = new Ext.LoadMask(this.bwrap,this.loadMask);
		//}



		if(this.rowdblclick) {
			this.on("rowdblclick", this.rowdblclick);
		}
		if(this.eventon) {
			var es = this.eventon;
			for(var e in es){
				this.on(e, es[e]);
			}
		}
		if(this.store) {
			this.store.load({params:{start:0, limit: this.pageSize}});
		}
		if(this.ready) {
			//alert(this.gridReader);
			(this.ready)();
		}
		

	},
	initStructure:function(){
		if(!Ext.isEmpty(this.tbar)) {
			this.getSelectionModel().on("selectionchange",function(){
				var selectid = this.getSelectedIds();
				

				//Ext.Msg.alert(selectid);
			}.createDelegate(this));
		}

		if(this.gridHeader) {
			if(this.checkbox) {
				this.sm = new Ext.grid.CheckboxSelectionModel();
				this.gridHeader.unshift(this.sm);
			}
			this.gridHeader.unshift(new Ext.grid.RowNumberer());
			this.cm = new Ext.grid.ColumnModel(this.gridHeader);
			this.cm.defaultSortable = true;
		}
		this.gridreader = new Ext.data.JsonReader({
			totalProperty:'totalProperty',
			root:'records'
		},this.gridStore);

		if(this.url) {
		
			this.store = new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({url:this.url}),
				reader: this.gridreader,
				baseParams: {limit: this.pageSize}
			});
		}
		if(this.page) {	// 设定分页
			this.bbar =  new Ext.PagingToolbar({
				pageSize : this.pageSize,
				store : this.store,
				displayInfo : true,
				displayMsg : c.page.pgholder,
				emptyMsg : c.page.nodata,
				items : [ '-' ]
			});
		}
	},

	getSelectedIds: function(keyField){
		var selectedRow = this.getSelectionModel().getSelections();
		var selectedIds = 0,ss = 0;
		if(Ext.isEmpty(keyField)) keyField = this.keyField;
		for(var i=0,len=selectedRow.length; i<len; i++){
			ss = parseInt(selectedRow[i].get(keyField));
			if(i==0)
				selectedIds = ss;
			else
				selectedIds = selectedIds + ',' + ss;
		}
		return selectedIds;
	},

	reload : function(str){
		var condition="";
		this.store.removeAll();
		if(typeof str == "object"){
			this.store.baseParams = str;
		}
		this.store.reload({params:{start:0, limit: this.pageSize}});
	}
});


